﻿using Quartz;
using Quartz.Impl;
using System;
using System.ServiceProcess;

namespace FileDeleteConsoleApp
{
    partial class Service1 : ServiceBase
    {
        private IScheduler sched;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                StdSchedulerFactory factory = new StdSchedulerFactory();
                var inItDataCron = System.Configuration.ConfigurationManager.AppSettings["InItDataCron"];
                sched = factory.GetScheduler();
                sched.Start();

                IJobDetail initDataJob = JobBuilder.Create<InitDataJob>()
                    .WithIdentity("initData", "group1")
                    .Build();

                ITrigger initDataTrigger = TriggerBuilder.Create()
                    .WithIdentity("initDataTrigger", "group1")
                    .StartNow()
                    .WithCronSchedule(inItDataCron)
                    .Build();

                sched.ScheduleJob(initDataJob, initDataTrigger);
               
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }

        protected override void OnStop()
        {
            sched?.Shutdown(waitForJobsToComplete: true);
        }
    }
}
